package com.summer.campussct.controller;


import com.summer.campussct.annotation.Permission;
import com.summer.campussct.pojo.VehicleOrder;
import com.summer.campussct.pojo.dto.RestResult;
import com.summer.campussct.service.IVehicleOrderService;
import com.summer.campussct.utils.PermissionConstant;
import com.summer.campussct.utils.UserIdHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Mr.Chen
 * @since 2024-05-15
 */
@Api(tags = "订单相关API")
@RestController
@RequestMapping("/order")
public class VehicleOrderController {

    @Resource
    private IVehicleOrderService vehicleOrderService;

    @ApiOperation("创建订单")
    @PostMapping("create")
    @Permission(PermissionConstant.LOGIN)
    public RestResult createOrder(@RequestBody @Validated VehicleOrder vehicleOrder){
        return vehicleOrderService.createOrder(vehicleOrder);

    }

    /**
     * 根据订单Id查询待支付金额和截至时间
     * @param id 订单id
     * @return 只有状态为待支付才能查到
     */
    @ApiOperation("查询订单待支付金额及支付截至时间")
    @GetMapping("payDetails/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult payDetails(@PathVariable("id") long id){
        return vehicleOrderService.payDetails(id);
    }

    /**
     * 不传uid，而是通过UserIdHolder获取，确保只能访问本人的订单
     * @param orderStatus
     * @return
     */
    @ApiOperation("获取用户的订单列表")
    @GetMapping("list")
    @Permission(PermissionConstant.LOGIN)
    public RestResult getOrderList(@RequestParam(required = false) Integer orderStatus){
        long uid = UserIdHolder.getId();
        return vehicleOrderService.getOrderList(uid,orderStatus);
    }

    /**
     * 根据订单id修改支付状态为已支付
     * @param id 订单id
     */
    @ApiOperation("支付完成通知接口")
    @PutMapping("paySuccess/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult paySuccess(@PathVariable("id") long id){
        return vehicleOrderService.paySuccess(id);
    }

    /**
     * @param id 订单id
     */
    @ApiOperation("取消订单")
    @PutMapping("cancelOrder/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult cancelOrder(@PathVariable("id") long id){
        return vehicleOrderService.cancelOrder(id);
    }

    /**
     * 根据订单id获取订单详情
     * @param id 订单id
     */
    @ApiOperation("获取订单详情")
    @GetMapping("details/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult getDetails(@PathVariable("id") long id){
        return vehicleOrderService.getDetails(id);
    }

    /**
     * @param id 订单id
     */
    @ApiOperation("完成交易")
    @PutMapping("finish/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult finishOrder(@PathVariable("id") long id){
        return vehicleOrderService.finishOrder(id);
    }

    /**
     * @param id 订单id
     */
    @ApiOperation("退款")
    @PutMapping("refund/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult refundOrder(@PathVariable("id") long id){
        return vehicleOrderService.refundOrder(id);
    }

    /**
     * @param id 订单id
     */
    @ApiOperation("取消退款")
    @PutMapping("cancelRefund/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult cancelRefundOrder(@PathVariable("id") long id){
        return vehicleOrderService.cancelRefundOrder(id);
    }

    /**
     * @param id 订单id
     */
    @ApiOperation("同意退款")
    @PutMapping("agreeRefund/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult agreeRefundOrder(@PathVariable("id") long id){
        return vehicleOrderService.agreeRefundOrder(id);
    }

    @ApiOperation("删除订单")
    @DeleteMapping("delete/{id}")
    @Permission(PermissionConstant.LOGIN)
    public RestResult deleteOrder(@PathVariable("id") long id){
        return vehicleOrderService.deleteOrder(id);
    }

    @ApiOperation("查询我卖出的订单页")
    @GetMapping("mySell")
    @Permission(PermissionConstant.LOGIN)
    public RestResult mySellOrder(@RequestParam(required = false) Integer orderStatus){
        long uid = UserIdHolder.getId();
        return vehicleOrderService.mySellOrder(uid, orderStatus);
    }
}

